layui.extend({
    dtree: '/system/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
}).use(['dtree','layer','jquery'], function(){
    	var  $=layui.$, 
    	layer = parent.layer === undefined ? layui.layer : top.layer,
    	dtree = layui.dtree;
    	var roleId = $("#roleId").val();
    	setTimeout(function(){
    	  layer.closeAll('loading');
    	},300); 
    	$.ajax({
			 url: "/dtree/getTreeList",
             type: "POST",
             traditional: true,
             asycn:false,
             data:{"roleId":roleId}, 
             dataType:"json",
             success: function (data) {
            	 console.log("data"+data);
            	 var zNodes=eval(data); 
            	 var DemoTree = dtree.render({
            		 elem: "#demoTree",
            		 data:zNodes,
            		 type:"all",
            		 dot:false,
            		 checkbar:true,
            		 checkbarData: "change",
            		 checkbarType:"all",
            		 checkbarFun: {
            			 chooseBefore: function($i, node){
            				 var flag = true;
            				 if(node.isLeaf == false){
            					 flag = false;
            					 if(node.ischecked == "0"){
            						 node.ischecked="1";
            						 DemoTree.checkStatus($i).check();  // 先将当前节点选中
            						 layer.confirm("确认授予全部权限？",{icon:3},function(index){
            							 console.log(node);
            							 //弹出loading
            						     var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            							 grant(node,index);
            							 DemoTree.checkStatus($i).noCheck();  // 恢复选中状态
            							 DemoTree.changeCheck();  // 真正根据选中关系选中
            						 },function(index){
            							 layer.msg("已取消");
            							 DemoTree.checkStatus($i).noCheck();
            						 });
            					 } else if(node.ischecked == "1") {
            						 node.ischecked="0";
            						 DemoTree.checkStatus($i).noCheck();
            						 layer.confirm("确认取消全部权限？",{icon:3},function(index){
            							 console.log(node);
            							 var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            							 grant(node,index);
            							 DemoTree.checkStatus($i).check();
            							 DemoTree.changeCheck();
            						 },function(index){
            							 layer.msg("已取消");
            							 DemoTree.checkStatus($i).check();
            						 });
            					 }
            				 }else{
            					 node.ischecked=node.ischecked == "0"?"1":"0";
            					 var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
            					 grant(node,index);
            				 }	
            				 return flag;  // 注意，如果执行了某些操作，此处必须为false，否则，默认返回true。
            			 }
            		 }
            	 });
             }
		 });
	 function grant(param,page){
		 var roleId = $("#roleId").val();
		 $.ajax({
              type: "POST",
              traditional: true,
              data:{"roleId":roleId,"data":JSON.stringify(param)},  
              url: "/dtree/toAdd",
              success: function (data) {
              	console.log("json"+JSON.stringify(param));
            	  setTimeout(function(){
                      var index = parent.layer.getFrameIndex(window.name);
                      parent.layer.close(index);
                	  layer.msg(data.msg, {time: 1000});
                  },700);
              }
    	});
	 }
});